package com.hyj.jpdy.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.hyj.jpdy.dao.ICommonDAO;
@Repository
public class CommonDAO implements ICommonDAO{

	@Autowired
	@Qualifier("jdbcTemplate")
    private JdbcTemplate jdbc;
	
	@Resource(name="sqlSession")
    public  SqlSessionTemplate sqlSession;
	
	
	public List<Map<String, Object>> queryForList(String sql,Object[] args){
    	List<Map<String, Object>> list = 
    		this.jdbc.queryForList(sql, args);
        return list;
    }
	
	public List<Map<String, Object>> queryForList(String sql){
    	List<Map<String, Object>> list = 
    		this.jdbc.queryForList(sql);
        return list;
    }
	
	public List<Map<String, Object>> queryForList(String sql,int pageNo,int pageSize,Object[] args){
		int  offset = (pageNo-1)*pageSize;
		String pageSql = sql + " LIMIT  "+offset+","+pageSize;
    	List<Map<String, Object>> list = 
    		this.jdbc.queryForList(pageSql, args);
        return list;
    }
	
	public int queryCount(String sql,Object[] args){
		String countSql = "  SELECT  COUNT(1) FROM ( "+sql+" ) c";
		return  this.jdbc.queryForInt(countSql, args);
	}

	public int update(String sql, Object[] args) {
		return  this.jdbc.update(sql, args);
		 
	}

	public int queryForInt(String sql, Object[] args) {
		return this.jdbc.queryForInt(sql, args);
	}

	public int queryForInt(String sql) {
		return this.jdbc.queryForInt(sql);
	}
	
	public int[] batchUpdate(String sql,List<Object[]> list){
	  return 	this.jdbc.batchUpdate(sql, list);
	}
	
	public <T>T queryForObject(String sql,Object[] args,Class<T> s){
		try {
		 return this.jdbc.queryForObject(sql, s, args);
		} catch (EmptyResultDataAccessException e) {
            return null;
		}
	}
	
	public <T>T queryForObjectIbatis(String sql,Object[] args,Class<T> s){
		try {
		 return this.sqlSession.selectOne("");
		} catch (EmptyResultDataAccessException e) {
            return null;
		}
	}
}
